<template>
  <div>
    <!-- 头部 -->
    <header>
      <div class="top">
        <p>地址管理</p>
      </div>
    </header>

    <!-- 地址 -->
    <van-address-list
     v-model="list.id"
      :list="list"
      disabled-text="以下地址超出配送范围"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="onitem"
    />
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
// import { Toast } from "vant";
import { addresslist } from "../../request/api.js";
export default {
  props: [],
  components: {},
  data() {
    return {
      userInfo: sessionStorage.getItem("user")
        ? JSON.parse(sessionStorage.getItem("user"))
        : {},
      list: [],
    };
  },
  computed: {
    ...mapGetters({}),
  },
  methods: {
    ...mapActions({}),
    onAdd() {
      this.$router.push("/addorder");
    },
    onEdit(item, index) {
      // Toast("编辑地址:" + index);
      console.log(item,index)
      this.$router.push("/addorder?id="+item.id);
    },
    onitem(item,index){
      this.$router.push("/order")
      this.$cookies.set("address",item)
      console.log(index)
    }
  },
    mounted() {
      //获取地址列表
      addresslist({uid:this.userInfo.uid}
      ).then(res=>{
        console.log(res)
        //循环出对象里的数据进行名字修改
        let oo = res.data.list.map(item => {
          // console.log(item.username)
          let obj =  {
            name:item.username,
            tel:item.userphone,
            address:item.location+item.useraddress,
            isDefault:Boolean(item.status),
            id:item.id
          }
          return obj
        });
        //将修改后的值赋给list
        this.list = oo
      })
    },
  }
</script>
<style scoped>
header {
  background: #ff6040;
  height: 1.45rem;
}
header .top {
  height: 0.8rem;
  display: flex;
  color: #ffdfd8;
  align-items: center;
  padding: 0.3rem 0.12rem 0.12rem 0.33rem;
  box-sizing: border-box;
}
header .top p {
  font-size: 0.5rem;
  margin: 0.5rem 1.45rem 0 2.2rem;
}
</style>